package br.edu.univas.si.engs.view;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import br.edu.univas.si.engs.controller.Controller;
import br.edu.univas.si.engs.model.RentTO;
import br.edu.univas.si.engs.model.ReserveTO;





/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ReservarJFrame extends javax.swing.JDialog {
	private JPanel reservarjPanel;
	private JLabel dataentregajLabel;
	private JLabel datareservajLabel;
	private JLabel carrojLabel;
	private JFormattedTextField dataentregajFormattedTextField;
	private JLabel clientejLabel;
	
	private JTextField clientejTextField;
	private JFormattedTextField datareservajFormattedTextField;
	private JTextField carrojTextField;
	
	private JScrollPane jScrollPane1;
	
	private JTable reservajTable;
	
	private JPanel consultareservajPanel;
	private JPanel ConsultarreservajPanel;
	private JPanel butompanelreservarjPanel;
	private DefaultTableModel tableReservaModel;
	private JButton excluirjButton;
	private JButton alterarjButton;
	private JButton limparjButton;
	private JButton reservarjButton;
	
	
	
	
	public ReservarJFrame(Frame object, String modelo, int idCarro, String clienteNom, int idCliente, Object object2, boolean b) {
		super(object,modelo,b);
		initGUI(modelo,idCarro,clienteNom,idCliente);
		setLocationRelativeTo(null);  
	}
	

	public JPanel getReservarjPanel() {
		return reservarjPanel;
	}

	public JTextField getClientejTextField() {
		return clientejTextField;
	}

	public JLabel getCarrojLabel() {
		return carrojLabel;
	}

	public JScrollPane getjScrollPane1() {
		return jScrollPane1;
	}

	public JTable getReservajTable() {
		return reservajTable;
	}

	public JPanel getConsultareservajPanel() {
		return consultareservajPanel;
	}

	public JPanel getConsultarreservajPanel() {
		return ConsultarreservajPanel;
	}

	public JButton getExcluirjButton() {
		return excluirjButton;
	}

	public JButton getAlterarjButton() {
		return alterarjButton;
	}

	public JButton getLimparjButton() {
		return limparjButton;
	}

	public JButton getReservarjButton() {
		return reservarjButton;
	}

	public JPanel getButompanelreservarjPanel() {
		return butompanelreservarjPanel;
	}




	public JFormattedTextField getDataentregajFormattedTextField() {
		return dataentregajFormattedTextField;
	}

	public JLabel getDataentregajLabel() {
		return dataentregajLabel;
	}



	public JFormattedTextField getDatareservajFormattedTextField() {
		return datareservajFormattedTextField;
	}

	public JLabel getDatareservajLabel() {
		return datareservajLabel;
	}

	public JTextField getCarrojTextField() {
		return carrojTextField;
	}

	public JLabel getClientejLabel() {
		return clientejLabel;
	}


	
	
	private void initGUI(String modelo, final int idCarro, String clienteNom, final int idCliente) {
		
		try {

			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Efetuar reserva");
			{
				reservarjPanel = new JPanel();
				GroupLayout reservarjPanelLayout = new GroupLayout((JComponent)reservarjPanel);
				reservarjPanel.setLayout(reservarjPanelLayout);
				{
					clientejLabel = new JLabel();
					clientejLabel.setText("Cliente");
				}
				{
					clientejTextField = new JTextField();
					clientejTextField.setText(clienteNom);
					clientejTextField.setEditable(false);
						
				}
				{
					carrojLabel = new JLabel();
					carrojLabel.setText("Carro");
				}
				{
					carrojTextField = new JTextField();
					carrojTextField.setText(modelo);
					carrojTextField.setEditable(false);
				}
				{
					datareservajLabel = new JLabel();
					datareservajLabel.setText("Data reserva");
				}
				{
					datareservajFormattedTextField = new JFormattedTextField();
					//dataentregajFormattedTextField.requestFocus();
				}
				{
					dataentregajLabel = new JLabel();
					dataentregajLabel.setText("Data entrega");
				}
				reservarjPanelLayout.setHorizontalGroup(reservarjPanelLayout.createSequentialGroup()
					.addGap(7)
					.addGroup(reservarjPanelLayout.createParallelGroup()
					    .addComponent(dataentregajLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
					    .addComponent(datareservajLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
					    .addGroup(reservarjPanelLayout.createSequentialGroup()
					        .addGap(27)
					        .addGroup(reservarjPanelLayout.createParallelGroup()
					            .addComponent(clientejLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
					            .addGroup(GroupLayout.Alignment.LEADING, reservarjPanelLayout.createSequentialGroup()
					                .addGap(7)
					                .addComponent(carrojLabel, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)))))
					.addGap(8)
					.addGroup(reservarjPanelLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, reservarjPanelLayout.createSequentialGroup()
					        .addComponent(datareservajFormattedTextField, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 113, Short.MAX_VALUE))
					    .addComponent(carrojTextField, GroupLayout.Alignment.LEADING, 0, 251, Short.MAX_VALUE)
					    .addComponent(clientejTextField, GroupLayout.Alignment.LEADING, 0, 251, Short.MAX_VALUE)
					    .addGroup(GroupLayout.Alignment.LEADING, reservarjPanelLayout.createSequentialGroup()
					        .addComponent(getDataentregajFormattedTextFieldx(), GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 113, Short.MAX_VALUE)))
					.addContainerGap(122, 122));
				reservarjPanelLayout.setVerticalGroup(reservarjPanelLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(reservarjPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(clientejTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(clientejLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(reservarjPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(carrojTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(carrojLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(reservarjPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(datareservajFormattedTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(datareservajLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(reservarjPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(dataentregajLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(getDataentregajFormattedTextFieldx(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
					.addContainerGap());
			}
			{
				butompanelreservarjPanel = new JPanel();
				{
					reservarjButton = new JButton();
					butompanelreservarjPanel.add(getReservarjButton());
					reservarjButton.setText("Reservar");
					reservarjButton.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							
							
							//System.out.println("Aceito:"+locacaoAceita());
							//if(locacaoAceita(idCliente)){
							ReserveTO reservTO = new ReserveTO();
							reservTO=
							getController().reservar(AddContactReserva(idCliente, idCarro));
							
							//addReserva(getController().consultaLocacao(clientejTextField.getText()));
							
							}

						private ReserveTO AddContactReserva(int idCliente,int idCar) {
							
							ReserveTO rese = new ReserveTO();
							rese.setCliente(idCliente);
							rese.setCarro(idCar);
							rese.setDataReserva(getDatareservajFormattedTextField().getText());
							rese.setDataEntrega(getDataentregajFormattedTextFieldx().getText());
							
							return rese;
						}
						
					});
				}
				{
					limparjButton = new JButton();
					butompanelreservarjPanel.add(getLimparjButton());
					limparjButton.setText("Limpar");
				}
				{
					alterarjButton = new JButton();
					butompanelreservarjPanel.add(getAlterarjButton());
					alterarjButton.setText("Alterar");
				}
				{
					excluirjButton = new JButton();
					butompanelreservarjPanel.add(excluirjButton);
					excluirjButton.setText("Excluir");
				}
			}
			{
				ConsultarreservajPanel = new JPanel();
			}
			{
				consultareservajPanel = new JPanel();
				GroupLayout consultareservajPanelLayout = new GroupLayout((JComponent)consultareservajPanel);
				consultareservajPanel.setLayout(consultareservajPanelLayout);
				{
					jScrollPane1 = new JScrollPane();
					{
						reservajTable = new JTable();
						reservajTable.setModel(getTableReservaModel());
						jScrollPane1.setViewportView(reservajTable);
						reservajTable.setPreferredSize(new java.awt.Dimension(454, 14));
					}
				}
				consultareservajPanelLayout.setHorizontalGroup(consultareservajPanelLayout.createSequentialGroup()
					.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 457, GroupLayout.PREFERRED_SIZE));
				consultareservajPanelLayout.setVerticalGroup(consultareservajPanelLayout.createSequentialGroup()
					.addComponent(jScrollPane1, 0, 17, Short.MAX_VALUE)
					.addContainerGap(44, 44));
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(ConsultarreservajPanel, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
				        .addGap(133))
				    .addComponent(reservarjPanel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(butompanelreservarjPanel, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
				.addComponent(consultareservajPanel, 0, 83, Short.MAX_VALUE)
				.addContainerGap(45, 45));
			thisLayout.setHorizontalGroup(thisLayout.createParallelGroup()
				.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				    .addComponent(ConsultarreservajPanel, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
				    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				    .addGroup(thisLayout.createParallelGroup()
				        .addGroup(thisLayout.createSequentialGroup()
				            .addGap(0, 0, Short.MAX_VALUE)
				            .addComponent(consultareservajPanel, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE))
				        .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				            .addGap(201)
				            .addComponent(butompanelreservarjPanel, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
				            .addGap(0, 11, Short.MAX_VALUE)))
				    .addGap(12))
				.addComponent(reservarjPanel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 465, GroupLayout.PREFERRED_SIZE));

			pack();
			this.setSize(481, 347);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	


	public Controller getController() {
		Controller controller = new Controller(null, null, null, null, null);
		return controller;
	}


	private TableModel getTableReservaModel() {
		if(tableReservaModel == null){
			tableReservaModel = new DefaultTableModel();
			//tablelocacaoModel.fireTableStructureChanged();
			tableReservaModel.setColumnIdentifiers(new String[]{
					"Codigo","Carro","Cliente","Data Reserva","Hora Reserva","Data Entrega","Hora Entrega"
			});
		}
		return tableReservaModel;
	
		
	}
	
	public JFormattedTextField getDataentregajFormattedTextFieldx() {
		if(dataentregajFormattedTextField == null) {
			dataentregajFormattedTextField = new JFormattedTextField();
		}
		return dataentregajFormattedTextField;
	}

}
